﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;


/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class CarInfo_Result {
    [DataMember(Order=0)]
    public busReslut Bus{get;set;}   
}

[DataContract]
class busReslut
{
    [DataMember(Order = 0)]
    public string id { get; set; }

    [DataMember(Order = 1)]
    public int provider_id { get; set; }

    [DataMember(Order = 2)]
    public float lat { get; set; }

    [DataMember(Order = 3)]
    public float lng { get; set; }

    [DataMember(Order = 4)]
    public int route_id { get; set; }

    [DataMember(Order = 5)]
    public int goback { get; set; }

    [DataMember(Order = 6)]
    public string destination { get; set; }

    [DataMember(Order = 7)]
    public string from { get; set; }

    [DataMember(Order = 8)]
    public string to { get; set; }

    [DataMember(Order = 9)]
    public string nameZh { get; set; }

    [DataMember(Order = 10)]
    public string city { get; set; }

    [DataMember(Order = 11)]
    public string on_stop { get; set; }

    [DataMember(Order = 12)]
    public string obj_id { get; set; }

    [DataMember(Order = 13)]
    public string on_stop_id { get; set; }
}
public partial class Service_CarInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["bsid"]))
        {
            using (MyDb.Db db = new MyDb.Db())
            {
                    IList<CarInfo_Result> list = new List<CarInfo_Result>();
                    CarInfo_Result item = null;
                    busReslut bus = null;

                    using (DataTable dt = db.getDataTable(string.Format("select top 1 a.busid,a.ProviderID,a.Latitude,a.Longitude,a.RouteID,a.GoBack,b.departureZh,b.destinationZh,b.pathattributename,a.City,a.CarOnStop,c.SubjectID,b.departureZh,a.stopId from busevent a left join Route b on a.RouteID = b.pathAttributeId left join bus c on c.BusID=a.BusID  where a.BusID='{0}'", Request.QueryString["bsid"]), "mess"))
                    {
                        foreach (DataRow rs in dt.Rows)
                        {
                            item = new CarInfo_Result();
                            bus = new busReslut();
                            bus.id = rs["busid"].ToString();
                            bus.provider_id = Convert.ToInt16(rs["ProviderID"]);
                            bus.lat = float.Parse(rs["Latitude"].ToString());
                            bus.lng = float.Parse(rs["Longitude"].ToString());
                            bus.route_id = Convert.ToInt32(rs["routeId"]);
                            bus.goback = Convert.ToInt32(rs["goback"]);
                            bus.destination = (rs["goback"].ToString() == "0" ? rs["departureZh"].ToString() : rs["destinationZh"].ToString());
                            bus.from = rs["departureZh"].ToString();
                            bus.to = rs["destinationZh"].ToString();
                            bus.nameZh = rs["pathattributename"].ToString();
                            bus.city = rs["city"].ToString();
                            bus.on_stop = (rs["CarOnStop"].ToString() == "0" ? "進站中" : "離站中");
                            bus.obj_id = rs["SubjectID"].ToString();
                            bus.on_stop_id = rs["stopId"].ToString();
                            item.Bus = bus;

                            list.Add(item);
                        }
                    }
                    if (list.Count == 0) { responseEnd(); }
                    Response.Write(MyDb.JSON.stringify(list));
                    list.Clear();
                    list = null;
                
            }
        }

        if (!string.IsNullOrEmpty(Request.QueryString["obj_id"]) && string.IsNullOrEmpty(Request.QueryString["bsid"])) {
            try
            {
                using (MyDb.Db db = new MyDb.Db())
                {
                    IList<CarInfo_Result> list = new List<CarInfo_Result>();
                    CarInfo_Result item = null;
                    busReslut bus = null;

                    using (DataTable dt = db.getDataTable(string.Format("select top 1a.busid,a.ProviderID,a.Latitude,a.Longitude,a.RouteID,a.GoBack,b.destinationZh,b.pathattributename,a.City,a.CarOnStop,c.SubjectID,b.departureZh,a.stopId  from bus c left join busevent a on a.BusID=c.BusID left join Route b on a.RouteID=b.pathAttributeId  where SubjectID = '{0}'", Request.QueryString["obj_id"]), "mess"))
                    {
                        foreach (DataRow rs in dt.Rows)
                        {
                            item = new CarInfo_Result();
                            bus = new busReslut();
                            bus.id = rs["busid"].ToString();
                            bus.provider_id = Convert.ToInt16(rs["ProviderID"]);
                            bus.lat = float.Parse(rs["Latitude"].ToString());
                            bus.lng = float.Parse(rs["Longitude"].ToString());
                            bus.route_id = Convert.ToInt32(rs["routeId"]);
                            bus.goback = Convert.ToInt32(rs["goback"]);
                            bus.destination = (rs["goback"].ToString() == "0" ? rs["departureZh"].ToString() : rs["destinationZh"].ToString());
                            bus.from = rs["departureZh"].ToString();
                            bus.to = rs["destinationZh"].ToString();
                            bus.nameZh = rs["pathattributename"].ToString();
                            bus.city = rs["city"].ToString();
                            bus.on_stop = (rs["CarOnStop"].ToString() == "0" ? "進站中" : "離站中");
                            bus.obj_id = rs["SubjectID"].ToString();
                            bus.on_stop_id = rs["stopId"].ToString();
                            item.Bus = bus;

                            list.Add(item);
                        }
                    }
                    if (list.Count == 0) { responseEnd(); }
                    Response.Write(MyDb.JSON.stringify(list));
                    list.Clear();
                    list = null;
                }
            }
            catch (Exception ex) {
                responseEnd();
            }
        }

        if (string.IsNullOrEmpty(Request.QueryString["obj_id"]) && string.IsNullOrEmpty(Request.QueryString["bsid"])) {
            responseEnd();
        }
        
    }

    public void responseEnd() {
        Response.Write("[{\"Bus\":{}}]");
        Response.End();
    }
}
